//
//  UIImageView+QSExtension.swift
//  TestSwift
//
//  Created by Song on 2018/4/25.
//  Copyright © 2018年 Song. All rights reserved.
//

import UIKit
import Kingfisher

extension UIImageView {
    /// 设置图片
    ///
    /// - Parameters:
    ///   - imgName: 图片名
    ///   - placeholder: 占位图片
    func qs_setImage(with imgName: String, placeholder: String) {
        if imgName.isEmpty {
            self.image = UIImage.init(named: placeholder)
            return
        }
        
        // 网络图片
        if imgName.hasPrefix("http://") || imgName.hasPrefix("https://") || imgName.hasPrefix("/") {
            var urlStr = imgName
            
            if imgName.hasPrefix("/") && HOST.hasSuffix("/") {
                let startIndex = urlStr.startIndex
                urlStr.remove(at: startIndex)
                urlStr = HOST + urlStr
            }
            
            self.kf.setImage(with: ImageResource.init(downloadURL: URL.init(string: urlStr)!), placeholder: UIImage.init(named: placeholder), options: [KingfisherOptionsInfoItem.transition(ImageTransition.fade(0.35))], progressBlock: { (receivedSize, totalSize) in
                
            }) { (result) in
                
            }
        } else {
            self.image = UIImage.init(named: imgName)
        }
    }
}
